/*
Autor: Alexander Savochkin, 2010
New BSD License
*/

#pragma once

#include "Config.h"
#include "IOperatedInput.h"
#include "IOperatedOutput.h"
#include "SharedResourceIO.h"

#include <boost/asio/serial_port.hpp>
#include <boost/none.hpp>

using std::string;
using boost::optional;
using boost::none;

using SRCLF::Config::Section;

namespace SRCLF 
{
namespace IO
{
namespace Serial
{
	/**
	@class SerialPort
	represents serial port connection
	*/  
	class SerialPort
	{
		static const char* file_name_key; // "file_name"
		static const char* baud_rate_key; // "baud_rate"
		static const char* character_size_key; // "character_size"
		static const char* parity_key; // "parity";
		static const char* flow_control_key; // "flow";

	public:
		SerialPort(const Section& config_section);
		~SerialPort(); //close serial connection
	private:
		//boost::asio::io_service &io_service;
		boost::asio::serial_port serial_port;
	};


	/**
	@class AbstractSerialInput
	represents input with searial interface. Dervide class should define getInput method, which
	depends on particular protocol over rs-232.
	*/
	class AbstractSerialInput : public IOperatedInput
	{
		optional<SerialPort> serial_port;

	public:
		AbstractSerialInput(const string& name, const Configuration& config);
		~AbstractSerialInput();
		//
		virtual bool open();
		virtual void close();
	};

	/**
	@class AbstractSerialOutput
	represents input with searial interface. Derived class should define getOutput and setOutput methods, which
	depends on particular protocol over rs-232.
	*/
	class AbstractSerialOutput : public IOperatedOutput
	{
		optional<SerialPort> serial_port;

	public:
		AbstractSerialOutput(const string& name, const Configuration& config);
		~AbstractSerialOutput();
		//
		virtual bool open();
		virtual void close();
	};

	//---------------------Shared serial input/output (several inputs and outputs share single com port) --------------------
	typedef SharedResourceInput<SerialPort> AbstractSharedSerialInput;
	typedef SharedResourceOutput<SerialPort> AbstractSharedSerialOutput;
} //namespace Serial
} //namespace IO
} //namespace SRCLF
